/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//		File Name		:		CAnimation.h
//
//		Author			:		Vindicated Games
//
//		Purpose			:		A header for all functions and members of the Animation class
//
//		Dates			:		Written( Feb 24, 2011 ) by Vindicated Games		LastUpdate( Feb 24, 2011  ) by Vindicated Games 
//	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "CAnimation.h"
#include "../tinyxml/tinyxml.h"
CAnimation::CAnimation()
{

	ImageID = -1;

	m_nCurFrame = 0;
	m_bIsPlaying = false;
	m_fSpeed= 0.0f;
	IsLooping = true;
	timeDelay = 0;
}

CAnimation::~CAnimation()
{
	if(!vFrames.empty())
	{
		vFrames.clear();
	}
	
}

CAnimation& CAnimation::operator=(const CAnimation &copy)
{
	ImageID = copy.ImageID;
	IsLooping = copy.IsLooping;
	m_bIsPlaying = copy.m_bIsPlaying;
	m_fSpeed = copy.m_fSpeed;
	m_nCurFrame = copy.m_nCurFrame;
	timeDelay = copy.timeDelay;
	for(int i = 0; i < (int)copy.vFrames.size(); i++)
	{
		Frame f;
		f.fTime = copy.vFrames[i].fTime;

		f.ptAnchor.x = copy.vFrames[i].ptAnchor.x;
		f.ptAnchor.y = copy.vFrames[i].ptAnchor.y;

		f.rAttack.left = copy.vFrames[i].rAttack.left;
		f.rAttack.top = copy.vFrames[i].rAttack.top;
		f.rAttack.right = copy.vFrames[i].rAttack.right;
		f.rAttack.bottom = copy.vFrames[i].rAttack.bottom;

		f.rRender.left = copy.vFrames[i].rRender.left;
		f.rRender.top = copy.vFrames[i].rRender.top;
		f.rRender.right = copy.vFrames[i].rRender.right;
		f.rRender.bottom = copy.vFrames[i].rRender.bottom;
		
		f.rCollision.left = copy.vFrames[i].rCollision.left;
		f.rCollision.top = copy.vFrames[i].rCollision.top;
		f.rCollision.right = copy.vFrames[i].rCollision.right;
		f.rCollision.bottom = copy.vFrames[i].rCollision.bottom;

		vFrames.push_back(f);

	}

	return *this;
}
void CAnimation::Reset()
{
	m_nCurFrame = 0;
	timeDelay = 0.0f;
}


void CAnimation::Play()
{
	//Reset();
	m_bIsPlaying = true;
}


void CAnimation::Resume()
{
	m_bIsPlaying = true;
}

void CAnimation::Stop()
{

	m_bIsPlaying = false;
}

void CAnimation::Render( float PosX, float PosY, bool IsFlipped, float scale, DWORD color)
{
	RECT rFrame = vFrames[m_nCurFrame].rRender;
	POINT pAnchor = vFrames[m_nCurFrame].ptAnchor;

	float fscaleX = scale;
	if(IsFlipped)
	{
		fscaleX = -fscaleX;
		int width = rFrame.right - rFrame.left;
		PosX += width * scale;
		pAnchor.x = width - pAnchor.x;
	}
	CSGD_TextureManager::GetInstance()->Draw(ImageID,int(PosX - (pAnchor.x * scale)), int(PosY - (pAnchor.y * scale)),
		fscaleX,scale,&rFrame,0,0,0,color);
}

void CAnimation::Update( float fElapsedTime )
{
	if(!m_bIsPlaying)
		return;
	timeDelay += fElapsedTime * m_fSpeed;

	if(timeDelay > vFrames[m_nCurFrame].fTime)
	{
		timeDelay -= vFrames[m_nCurFrame].fTime;
		m_nCurFrame++;
		if((unsigned int)m_nCurFrame >= vFrames.size())
		{
			if(IsLooping)
			{
				Reset();
			}
			else
			{
				Stop();
				m_nCurFrame = vFrames.size() - 1;
			}
		}
	}
}
bool CAnimation::Load( const char* AnimInfo)
{
	int tempID;

	TiXmlDocument doc;

	if(doc.LoadFile(AnimInfo) == false)
		return false;

	TiXmlElement* Root = doc.RootElement();
	if(Root == NULL)
		return false;

	TiXmlElement* NumAnim = Root->FirstChildElement("xNumOfAnim");
	int count = atoi(NumAnim->GetText());

	TiXmlElement* ImageFile = Root->FirstChildElement("xImageFile");
	if(ImageFile)
	{
		tempID = CSGD_TextureManager::GetInstance()->LoadTexture(ImageFile->GetText());
	}

	TiXmlElement* xAnimation  = Root->FirstChildElement("xAnimation");

	while(xAnimation)
	{
		CAnimation a;

		TiXmlElement* NumFrame = xAnimation->FirstChildElement("xNumOfFrames");
		int fcount = atoi(NumFrame->GetText());

		TiXmlElement* looping = xAnimation->FirstChildElement("xLooping");
		a.SetIsLooping((bool)(atoi(looping->GetText())));

		TiXmlElement* AnimSpd = xAnimation->FirstChildElement("xAnimSpd");
		a.SetSpeed((float)( atoi(AnimSpd->GetText())));

		TiXmlElement* xFrame = xAnimation->FirstChildElement("xFrame");

		Frame temp;
		temp.fTime = 0;
		temp.ptAnchor.x = 0;
		temp.ptAnchor.y = 0;

		SetRect(&temp.rAttack,0,0,0,0);
		SetRect(&temp.rCollision,0,0,0,0);
		SetRect(&temp.rRender,0,0,0,0);

		temp.ptAnchor.x = 0;
		temp.ptAnchor.y = 0;


		while(xFrame)
		{
			TiXmlElement* xTime = xFrame->FirstChildElement("xTime");
			if(xTime)
			{
				temp.fTime =(float)( atoi(xTime->GetText()) );
			}
			TiXmlElement* FrameType = xFrame->FirstChildElement("xFrameType");
			if(FrameType)
			{
				TiXmlElement* RenderFrame = FrameType->FirstChildElement("xRenderFrame");
				if(RenderFrame)
				{
					TiXmlElement* RenderFrameX = RenderFrame->FirstChildElement("xRenderFrameX");
					if(RenderFrameX)
						temp.rRender.left =  atoi(RenderFrameX->GetText());

					TiXmlElement* RenderFrameY = RenderFrame->FirstChildElement("xRenderFrameY");
					if(RenderFrameY)
						temp.rRender.top = atoi(RenderFrameY->GetText());

					TiXmlElement* RenderFrameW = RenderFrame->FirstChildElement("xRenderFrameW");
					if(RenderFrameW)
					{
						temp.rRender.right = atoi(RenderFrameW->GetText());
						temp.rRender.right += temp.rRender.left;
					}

					TiXmlElement* RenderFrameH = RenderFrame->FirstChildElement("xRenderFrameH");
					if(RenderFrameH)
					{
						temp.rRender.bottom = atoi(RenderFrameH->GetText());
						temp.rRender.bottom += temp.rRender.top;
					}
				}

				TiXmlElement* CollisionFrame = FrameType->FirstChildElement("xCollisionFrame");
				if(CollisionFrame)
				{
					TiXmlElement* CollisionFrameX = CollisionFrame->FirstChildElement("xCollisionFrameX");
					if(CollisionFrameX)
						temp.rCollision.left = atoi(CollisionFrameX->GetText());

					TiXmlElement* CollisionFrameY = CollisionFrame->FirstChildElement("xCollisionFrameY");
					if(CollisionFrameY)
						temp.rCollision.top = atoi(CollisionFrameY->GetText());

					TiXmlElement* CollisionFrameW = CollisionFrame->FirstChildElement("xCollisionFrameW");
					if(CollisionFrameW)
						temp.rCollision.right = atoi(CollisionFrameW->GetText());

					TiXmlElement* CollisionFrameH = CollisionFrame->FirstChildElement("xCollsionFrameH");
					if(CollisionFrameH)
						temp.rCollision.bottom = atoi(CollisionFrameH->GetText());
				}

				TiXmlElement* AttackFrame = FrameType->FirstChildElement("xAttackFrame");
				if(AttackFrame)
				{
					TiXmlElement* AttackFrameX = AttackFrame->FirstChildElement("xAttackFrameX");
					if(AttackFrameX)
						temp.rAttack.left = atoi(AttackFrameX->GetText());

					TiXmlElement* AttackFrameY = AttackFrame->FirstChildElement("xAttackFrameY");
					if(AttackFrameY)
						temp.rAttack.top = atoi(AttackFrameY->GetText());

					TiXmlElement* AttackFrameW = AttackFrame->FirstChildElement("xAttackFrameW");
					if(AttackFrameW)
						temp.rAttack.right = atoi(AttackFrameW->GetText());

					TiXmlElement* AttackFrameH = AttackFrame->FirstChildElement("xAttackFrameH");
					if(AttackFrameH)
						temp.rAttack.bottom = atoi(AttackFrameH->GetText());
				}

				TiXmlElement* AnchorPoint = FrameType->FirstChildElement("xAnchorPoint");
				if(AnchorPoint)
				{
					TiXmlElement* AnchorPointX = AnchorPoint->FirstChildElement("xAnchorPointX");
					if(AnchorPointX)
					{
						int ax = atoi(AnchorPointX->GetText());
						temp.ptAnchor.x =  ax - temp.rRender.left;
					}

					TiXmlElement* AnchorPointY = AnchorPoint->FirstChildElement("xAnchorPointY");
					if(AnchorPointY)
					{
						int ay = atoi(AnchorPointY->GetText());
						temp.ptAnchor.y = ay - temp.rRender.top;
					}
				}
			}
			a.SetTimeDelay(temp.fTime);

			temp.rAttack.right +=  temp.rAttack.left;
			temp.rAttack.bottom += temp.rAttack.top;
			temp.rAttack.left -= (temp.ptAnchor.x + temp.rRender.left);
			temp.rAttack.top -= (temp.ptAnchor.y + temp.rRender.top);
			temp.rAttack.right -= (temp.ptAnchor.x + temp.rRender.left);
			temp.rAttack.bottom -=(temp.ptAnchor.y + temp.rRender.top);



			temp.rCollision.right +=  temp.rCollision.left;
			temp.rCollision.bottom += temp.rCollision.top;
			temp.rCollision.left -= (temp.ptAnchor.x + temp.rRender.left);
			temp.rCollision.top -= (temp.ptAnchor.y + temp.rRender.top);
			temp.rCollision.right -= (temp.ptAnchor.x + temp.rRender.left);
			temp.rCollision.bottom -=(temp.ptAnchor.y + temp.rRender.top);


			a.addFrame(temp);
			xFrame = xFrame->NextSiblingElement("xFrame");
		}
		a.SetImageID(tempID);
		ImageID = tempID;
		vAnimation.push_back(a);

		xAnimation = xAnimation->NextSiblingElement("xAnimation");



	}



	return true;

}